﻿using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Globalization;

using System.Data.Sql;
using System.Data.SqlClient;

using System.Linq;
using System.Collections.Generic;


namespace WPAgendaAniversariante.VisualWebPart1
{
    [ToolboxItemAttribute(false)]
    public class WPAgendaAniversariante : System.Web.UI.WebControls.WebParts.WebPart
    {
        // Visual Studio might automatically update this path when you change the Visual Web Part project item.
        private const string _ascxPath = @"~/_CONTROLTEMPLATES/WPAgendaAniversariante/VisualWebPart1/VisualWebPart1UserControl.ascx";

        public WPAgendaAniversariante()
        {
        }

        string tipomes;

        protected override void CreateChildControls()
        {
            Control control = Page.LoadControl(_ascxPath);
            Controls.Add(control);

            base.CreateChildControls();

        }



        protected override void RenderContents(HtmlTextWriter writer)
        {
            try
            {
                DateTime agora = DateTime.Now;
                string recebe;
                recebe = System.Web.HttpContext.Current.Request.QueryString["f"];

                SPSite mySite = SPContext.Current.Site;
                SPWeb myWeb = mySite.OpenWeb();

                SPList listEvento = myWeb.Lists["Eventos"];
                SPList listUsuario = myWeb.Lists["Usuário"];

                CultureInfo mesExtensio = new CultureInfo("pt-BR");
                DateTimeFormatInfo dtif = mesExtensio.DateTimeFormat;

                for (int mes = 1; mes <= 12; mes++)
                {
                    if (recebe == "a")
                    {
                        var resultsUsuario = listUsuario.Items.Cast<SPListItem>().Where(i => Convert.ToDateTime(i["Data nascimento"]).Month.Equals(mes)).OrderBy(i => Convert.ToDateTime(i["Data nascimento"]).Day).ToList();
                        writer.Write("<div id=\"eventoMes" + mes + "\" style=\"display:none\">");
                        if (resultsUsuario.Count > 0)
                        {
                            writer.Write("<br>");
                            foreach (SPListItem itemUsuario in resultsUsuario)
                            {
                                if (!itemUsuario["Nome"].ToString().Contains("FAX"))
                                {
                                    writer.Write("<ul>");
                                    //                              writer.Write("<li><a href=\"/Paginas/usuario.aspx?ID_Lista=" + itemUsuario["ID"] + "&strLetra=" + itemUsuario["Nome"].ToString().Substring(0, 1) + "\">" + Convert.ToDateTime(itemUsuario["Data nascimento"]).Day.ToString() + " de " + mesExtensio.TextInfo.ToTitleCase(dtif.GetMonthName(Convert.ToInt32(mes))) + "</a>");
                                    //                              writer.Write("<span>" + itemUsuario["Nome"].ToString().ToUpper() + "</span></li>");
                                    writer.Write("<li>" + Convert.ToDateTime(itemUsuario["Data nascimento"]).Day.ToString() + " de " + mesExtensio.TextInfo.ToTitleCase(dtif.GetMonthName(Convert.ToInt32(mes))));
                                    writer.Write("<span><a href=\"/Paginas/usuario.aspx?ID_Lista=" + itemUsuario["ID"] + "&strLetra=" + itemUsuario["Nome"].ToString().Substring(0, 1) + "\">" + itemUsuario["Nome"].ToString().ToUpper() + "</a></span></li>");
                                    writer.Write("</ul>");
                                }
                            }
                        }
                        else
                        {
                            writer.Write("<br>");
                            writer.Write("<p>Nenhum registro encontrado.</p>");
                        }
                        writer.Write("</div>");

                    }
                    else
                    {
                        var resultsEvento = listEvento.Items.Cast<SPListItem>().Where(i => Convert.ToDateTime(i["Hora de Início"]).Month.Equals(mes) && Convert.ToDateTime(i["Hora de Início"]).Year.Equals(agora.Year)).OrderBy(i => i["Hora de Início"]).ToList();
                        if (agora.Month.Equals(mes))
                        {
                            resultsEvento = resultsEvento.Where(i => Convert.ToDateTime(i["Hora de Início"]).Day >= agora.Day || Convert.ToDateTime(i["Hora de Término"]).Day >= agora.Day).OrderBy(i => i["Hora de Início"]).ToList();
                        }
                        resultsEvento = resultsEvento.Take(3).ToList();


                        writer.Write("<div id=\"eventoMes" + mes + "\" style=\"display:none\">");

                        if (resultsEvento.Count > 0)
                        {
                            writer.Write("<br>");
                            foreach (SPListItem itemEvento in resultsEvento)
                            {
                                writer.Write("<ul>");
                                writer.Write("<li><a href=\"/Paginas/agenda_detalhe.aspx?i=" + itemEvento["ID"].ToString() + "\">" + Convert.ToDateTime(itemEvento["Hora de Início"]).Day.ToString() + " de " + mesExtensio.TextInfo.ToTitleCase(dtif.GetMonthName(Convert.ToInt32(mes))) + "</a>");
                                writer.Write("<span>" + itemEvento["Título"].ToString() + "</span></li>");
                                writer.Write("</ul>");
                            }
                        }
                        else
                        {
                            writer.Write("<br>");
                            writer.Write("<p>Nenhum registro encontrado.</p>");
                        }
                        writer.Write("</div>");
                    }
                }
            }
            catch (Exception e)
            {
                writer.Write("<br>");
                writer.Write("<p>Não Foi possivel conectar ao servidor.</p>");
            }

        }

    }
}





/*



*/